package com.smartEdu.service.impl.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.CourseCategoryDao;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.ContentModel;
import com.smartEdu.model.CourseCategoryModel;
import com.smartEdu.model.RecommendFriendModel;
import com.smartEdu.service.CourseCategoryService;

@Service(value="courseCategoryServiceImpl")
public class CourseCategoryServiceImpl extends BaseServiceImpl implements CourseCategoryService{
	
	@Autowired
	@Qualifier(value="courseCategoryDaoImpl")
	private CourseCategoryDao courseCategoryDao;

	@Override
	public void getCourseCategoryRoot(Session sess) {
		List<CourseCategoryModel> models=null;
	
		List model=new ArrayList<ContentModel>();
	
		
		CMap map=null;
		try {
			map=SetToMap(sess);
			models=courseCategoryDao.getCourseCategoryRoot();
			
			for(CourseCategoryModel coursategoryModel:models){
				ContentModel contentModel=new ContentModel();
				List<CourseCategoryModel> childmodel=null;	  
			    childmodel=courseCategoryDao.getCourseCategoryChidByPK(coursategoryModel.getPk().toString());
	            contentModel.setPk(coursategoryModel.getPk().toString());
	            contentModel.setName(coursategoryModel.getName().toString());
	            contentModel.setList(childmodel);
	            model.add(contentModel);
	            System.out.println(model.get(0));
				System.out.println(coursategoryModel.getPk().toString());
			}
			sess.put("genre", model);
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("CourseCategory业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("CourseCategory业务流程出错");
		}
		finally {
			map=null;
		}
	}

	@Override
	public void getCourseCategoryChid(Session sess) {
		List<CourseCategoryModel> models=null;
		CMap map=null;
		try {
			map=SetToMap(sess);
			if (map.getVals().containsKey("id")) {
				/*
				 * 此处根据学生信息推荐相关课程
				 */
				models=courseCategoryDao.getCourseCategoryChid();
				sess.put("genre", models);
			}
			if (map.getVals().containsKey("pk")){
				models=courseCategoryDao.getCourseCategoryChidByPK(map.gets("pk"));
				sess.put("genre", models);
			}
			
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.OK);
		}
		catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("CourseCategory业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("CourseCategory业务流程出错");
		}
		finally {
			map=null;
		}
	}
	
}
